const app = getApp();
import { formatters, hint } from "../../../utils/util";
import Apipublic from "../../../models/api";
import { Cars } from "../../../models/cars";
const api = new Apipublic();
const cars = new Cars();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl: getApp().globalImgUrl.config.baseImgUrl,
    form: {},
    carNumber: "",
    currentDate: new Date().getTime(),
    minDate: new Date("1970-01-01").getTime(),
    maxDate: new Date().getTime(),
    formatter(type, value) {
      if (type === "year") {
        return `${value}年`;
      }
      if (type === "month") {
        return `${value}月`;
      }
      if (type === "day") {
        return `${value}日`;
      }
      return value;
    },
    producer: "",
    producerDesc: "",
    brandShow: false,
    colorShow: false,
    typeShow: false,
    timeShow: false,
    producerShow: false,
    brandList: [],
    colorList: [],
    typeList: [],
    producerList: [],
  },
  onShow() {
    api.dictItemList("car_brand").then((res) => {
      this.setData({
        brandList: res.result || [],
      });
    });
    api.dictItemList("car_color").then((res) => {
      this.setData({
        colorList: res.result || [],
      });
    });
    api.dictItemList("car_type").then((res) => {
      this.setData({
        typeList: res.result || [],
      });
    });
    api.dictItemList("country_of_origin").then((res) => {
      this.setData({
        producerList: res.result || [],
      });
    });
  },
  handlerGobackClick() {
    wx.navigateBack();
  },
  //打开弹出框
  handleShowPicker(e) {
    let name = e.currentTarget.dataset.name;
    this.setData({
      [name]: true,
    });
  },
  //关闭/取消弹出框
  closePopup(e) {
    let name = e.currentTarget.dataset.name;
    this.setData({
      [name]: false,
    });
  },
  carNumberChange(e) {
    let form = this.data.form;
    let carNumber = e.detail.toUpperCase();
    form = { ...form, carNumber };
    this.setData({
      form,
      carNumber,
    });
  },
  showConfirm1(e) {
    let name = e.currentTarget.dataset.name;
    let obj = e.detail.value;
    let form = this.data.form;
    if (obj) {
      let carBrand = obj.value;
      let carBrand_dictText = obj.text;
      form = { ...form, carBrand, carBrand_dictText };
      this.setData({
        form,
        [name]: false,
      });
    } else {
      this.setData({
        [name]: false,
      });
    }
  },
  showConfirm2(e) {
    let name = e.currentTarget.dataset.name;
    let obj = e.detail.value;
    let form = this.data.form;
    if (obj) {
      let carColor = obj.value;
      let carColor_dictText = obj.text;
      form = { ...form, carColor, carColor_dictText };
      this.setData({
        form,
        [name]: false,
      });
    } else {
      this.setData({
        [name]: false,
      });
    }
  },
  showConfirm3(e) {
    let name = e.currentTarget.dataset.name;
    let obj = e.detail.value;
    let form = this.data.form;
    if (obj) {
      let carType = obj.value;
      let carType_dictText = obj.text;
      form = { ...form, carType, carType_dictText };
      this.setData({
        form,
        [name]: false,
      });
    } else {
      this.setData({
        [name]: false,
      });
    }
  },
  showConfirm4(e) {
    let name = e.currentTarget.dataset.name;
    let currentDate = e.detail;
    let selectTime = formatters(e.detail, "YY-MM-DD");
    let form = this.data.form;
    if (selectTime) {
      let carBirthday = selectTime;
      form = { ...form, carBirthday };
      this.setData({
        currentDate: currentDate,
        form,
        [name]: false,
      });
    } else {
      this.setData({
        [name]: false,
      });
    }
  },
  showConfirm5(e) {
    let name = e.currentTarget.dataset.name;
    let obj = e.detail.value;
    let form = this.data.form;
    if (obj) {
      let carCountry = obj.value;
      let carCountry_dictText = obj.text;
      form = { ...form, carCountry, carCountry_dictText };
      this.setData({
        form,
        [name]: false,
      });
    } else {
      this.setData({
        [name]: false,
      });
    }
  },
  blurChange(e) {
    let value = e.detail.value;
    if (value.length == 7) {
      var express =
        /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/;
      if (!express.test(value)) {
        hint("请输入正确车牌号");
        return;
      }
    } else {
      hint("请输入正确车牌号");
      return;
    }
  },
  addCar() {
    let commid = wx.getStorageSync("commid");
    let commName = wx.getStorageSync("commName");
    let {
      carNumber,
      carBrand_dictText,
      carColor_dictText,
      carType_dictText,
      carBirthday,
      carCountry_dictText,
    } = this.data.form;
    if (!carNumber) {
      hint("请输入车牌号");
      return;
    }
    if (!carBrand_dictText) {
      hint("请选择车辆品牌");
      return;
    }
    if (!carColor_dictText) {
      hint("请选择颜色");
      return;
    }
    if (!carType_dictText) {
      hint("请选择类型");
      return;
    }
    if (!carBirthday) {
      hint("请选择购买时间");
      return;
    }
    if (!carCountry_dictText) {
      hint("请选择出产国");
      return;
    }
    let obj = { ...this.data.form, commid, commName };
    cars.addEdit(obj).then((res) => {
      hint(res.messagebody);
      wx.navigateBack();
    });
  },
});
